﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using VOD.Models;
using System.Web.Routing;

namespace VOD.Areas.Admin.Controllers
{
    [Authorize(Roles="Administrators")]
    public class UserMgrController : Controller
    {

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        //
        // GET: /Admin/UserMgr/
        public ActionResult Index()
        {
            var users = Membership.GetAllUsers();
            return View(users);
        }

        /// <summary>
        /// 在线用户
        /// </summary>
        /// <returns></returns>
        public ActionResult OnlineUsers()
        {
            var allUsers = Membership.GetAllUsers();
            var users=new MembershipUserCollection();
            foreach (MembershipUser user in allUsers)
            {
                if (user.IsOnline)
                {
                    users.Add(user);
                }
            }
            return View(users);
        }

        public ActionResult Details(string user)
        {
            if (user==null)
            {
                return RedirectToAction("Index");
            } 
            else
            {
                MembershipUser userDetails = Membership.GetUser(username:user);
                return View(userDetails);
            }
        }

        public ActionResult OutDate()
        {
            var allUsers = Membership.GetAllUsers();
            var users = new MembershipUserCollection();
            foreach (MembershipUser user in allUsers)
            {
                if (user.LastActivityDate.AddYears(1)<DateTime.Now)
                {
                    users.Add(user);
                }
            }
            return View(users);
        }

        public ActionResult ChangePassword()
        {
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
                {
                    ViewBag.Success = "密码修改成功。";
                    return View();
                }
                else
                {
                    ModelState.AddModelError("", "当前密码不正确或新密码无效。");
                }
            }
            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }

//         protected override void Dispose(bool disposing)
//         {
//             db.Dispose();
//             base.Dispose(disposing);
//         }
    }
}
